//编写一个客户端程序，对文件存储子模块进行测试
//1.封装四个接口进行rpc调用，实现对于四个业务接口的测试

#include "etcd.hpp"
#include "channel.hpp"
#include <gflags/gflags.h>
#include <thread>
#include <gtest/gtest.h>
#include "utils.hpp"
#include "logger.hpp"
#include "file.pb.h"
#include "base.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(file_service, "/service/file_service", "服务监控根目录");

zrb::ServiceChannel::ChannelPtr channel;

std::string file_id;

TEST(put_test,single_file)
{
    //1.读取当前目录下的指定文件数据
    std::string body;
    ASSERT_TRUE(zrb::readFile("Makefile",body));
    //2.实例化rpc调用客户端对象，发起rpc调用
    zrb::FileService_Stub stub(channel.get());
    zrb::PutSingleFileReq req;
    req.set_request_id("111");
    req.mutable_file_data()->set_file_name("Makefile");
    req.mutable_file_data()->set_file_size(body.size());
    req.mutable_file_data()->set_file_content(body);

    brpc::Controller *cntl = new brpc::Controller();
    zrb::PutSingleFileRsp *rsp = new zrb::PutSingleFileRsp();
    stub.PutSingleFile(cntl, &req, rsp, nullptr);

    //3.检测返回值中上传是否成功
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success()); //必须为真
    ASSERT_EQ(body.size(),rsp->file_info().file_size()); //必须相等
    ASSERT_EQ("Makefile",rsp->file_info().file_name());
    file_id = rsp->file_info().file_id();
    LOG_DEBUG("文件的id是{}",file_id);
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

TEST(get_test,single_file)
{
    //1.实例化rpc调用客户端对象，发起rpc调用
    zrb::FileService_Stub stub(channel.get());
    zrb::GetSingleFileReq req;
    req.set_request_id("222");
    req.set_file_id(file_id);

    brpc::Controller *cntl = new brpc::Controller();
    zrb::GetSingleFileRsp *rsp = new zrb::GetSingleFileRsp();
    stub.GetSingleFile(cntl, &req, rsp, nullptr);

    //2.检测返回值中下载是否成功
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
    ASSERT_EQ(req.request_id(),rsp->request_id());
    ASSERT_EQ(file_id,rsp->file_data().file_id());

    //3.将文件存储到当前系统下
    zrb::writeFile("signal_download_file",rsp->file_data().file_content());
}

std::vector<std::string> mutil_file_id(2);

TEST(put_test,multi_file)
{
    //1.读取当前目录下的指定文件数据
    std::string body1;
    ASSERT_TRUE(zrb::readFile("file.pb.h",body1));

    std::string body2;
    ASSERT_TRUE(zrb::readFile("file.pb.cc",body2));
    //2.实例化rpc调用客户端对象，发起rpc调用
    zrb::FileService_Stub stub(channel.get());
    zrb::PutMultiFileReq req;
    req.set_request_id("333");

    auto file_data = req.add_file_data();
    file_data->set_file_name("file.pb.h");
    file_data->set_file_size(body1.size());
    file_data->set_file_content(body1);

    file_data = req.add_file_data();
    file_data->set_file_name("file.pb.cc");
    file_data->set_file_size(body2.size());
    file_data->set_file_content(body2);

    brpc::Controller *cntl = new brpc::Controller();
    zrb::PutMultiFileRsp *rsp = new zrb::PutMultiFileRsp();
    stub.PutMultiFile(cntl, &req, rsp, nullptr);

    //3.检测返回值中上传是否成功
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success()); //必须为真
    for(int i = 0; i < rsp->file_info_size(); ++i)
    {
        LOG_DEBUG("文件的id是{}",file_id);
        mutil_file_id[i] = rsp->file_info(i).file_id();
    }
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

TEST(get_test,multi_file)
{
    //1.实例化rpc调用客户端对象，发起rpc调用
    zrb::FileService_Stub stub(channel.get());
    zrb::GetMultiFileReq req;
    req.set_request_id("444");
    req.add_file_id_list(mutil_file_id[0]);
    req.add_file_id_list(mutil_file_id[1]);

    brpc::Controller *cntl = new brpc::Controller();
    zrb::GetMultiFileRsp *rsp = new zrb::GetMultiFileRsp();
    stub.GetMultiFile(cntl, &req, rsp, nullptr);

    //2.检测返回值中下载是否成功
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());

    //3.将文件存储到当前系统下
    ASSERT_TRUE(rsp->file_data().find(mutil_file_id[0]) != rsp->file_data().end());
    ASSERT_TRUE(rsp->file_data().find(mutil_file_id[1]) != rsp->file_data().end());
    auto map = rsp->file_data();
    zrb::writeFile("mutil_download_file1",map[mutil_file_id[0]].file_content());
    zrb::writeFile("mutil_download_file2",map[mutil_file_id[1]].file_content());
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    zrb::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1. 先构造Rpc信道管理对象
    auto sm = std::make_shared<zrb::ServiceManager>();
    sm->declared(FLAGS_file_service);
    auto put_cb = std::bind(&zrb::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&zrb::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    //2. 构造服务发现对象
    zrb::Discovery::ptr dclient = std::make_shared<zrb::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    //3. 通过Rpc信道管理对象，获取提供Echo服务的信道
    channel = sm->choose(FLAGS_file_service);
    if (!channel) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return -1;
    }
    //4. 发起EchoRpc调用（通过rpc信道）



    return RUN_ALL_TESTS();;
}


